/*
Copyright (c) 2012 VBChunguk

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef __DANMAKUCOMPILER__
#define __DANMAKUCOMPILER__

#include <fstream>
#include <string>
#include <vector>
using namespace std;

#include <stdint.h>
#include "StringFunc.hpp"

namespace Danmaku
{
	class DanmakuCompiler;
	class DanmakuCompilerException;

	typedef struct {
		string name;
		uint32_t relpos;
	} DanmakuCompilerLabel;
	typedef struct {
		string name;
		uint32_t pos;
		bool coded;
	} DanmakuCompilerEntry;
	typedef struct {
		string name;
		uint32_t len;
		uint32_t pos;
		int scope;
		char *DefValue;
	} DanmakuVariable;
#pragma pack(push,1)
	typedef struct
	{
		uint16_t ID;
		uint16_t prop;
		uint16_t len;
		char *data;
	} Instruction;
#pragma pack(pop)
#pragma pack(push,1)
	typedef struct
	{
		uint32_t position;
		uint8_t namelen;

		string name;
	} EntryData;
#pragma pack(pop)
#pragma pack(push,1)
	typedef struct
	{
		char magic[4];
		uint32_t EntryPos;
		uint32_t DataPos;
		uint32_t CodePos;
	} ScriptHeader;
#pragma pack(pop)
#pragma pack(push,1)
	typedef struct
	{
		char name[4];
		uint32_t length;
		uint32_t position;
		uint32_t zero;

		char *description;
		char *data;
	} SectionHeader;
#pragma pack(pop)
#pragma pack(push,1)
	typedef union
	{
		uint32_t _int;
		float _float;
	} int_float;
#pragma pack(pop)

	class DanmakuCompiler
	{
	private:
		fstream SourceCode;
		vector<DanmakuCompilerEntry> EntryList;
		vector<DanmakuCompilerLabel> LableList;
		vector<Instruction> InsList;
		vector<DanmakuVariable> VarList;
		uint32_t nowpos;
		uint32_t varnowpos;

		bool ValueParse(string value, uint32_t &result);
		void MakeInstruction(uint16_t id, uint16_t prop, uint16_t len, char *data);
		void BuildMagic(char *target, const char *magic);
	public:
		DanmakuCompiler() {
			EntryList.clear();
			LableList.clear();
			InsList.clear();
			VarList.clear();
		}
		~DanmakuCompiler() {
			if(SourceCode.is_open()) SourceCode.close();
		}

		bool OpenFile(string path);
	};

	class DanmakuCompilerException
	{
	private:
		int code;
	public:
		DanmakuCompilerException() { code = unknown; }
		DanmakuCompilerException(int error) { code = error; }
		~DanmakuCompilerException() {}

		int getCode() const { return code; }

		static const int ok = 0;
		static const int unknown = -1;
		static const int errEntry = 1;
		static const int EntryAlreadyExists = 2;
		static const int InstructionArg = 3;
	};
}

#endif
